<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="月份" prop="mid">
        <!-- <el-input v-model="queryParams.mid" placeholder="请输入月份" clearable @keyup.enter.native="handleQuery" /> -->
        <el-select v-model="queryParams.mid" placeholder="请选择月份" filterable>
          <el-option v-for="item in monthList" :key="item.mid" :label="item.mname" :value="item.mid"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类别" prop="cid">
        <!-- <el-input v-model="queryParams.cid" placeholder="请输入类别" clearable @keyup.enter.native="handleQuery" /> -->
        <el-select v-model="queryParams.cid" placeholder="请选择类别">
          <el-option v-for="item in categoryList" :key="item.cid" :label="item.cname" :value="item.cid"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="地区" prop="rid">
        <!-- <el-input v-model="queryParams.rid" placeholder="请输入地区" clearable @keyup.enter.native="handleQuery" /> -->
        <el-select v-model="queryParams.rid" placeholder="请选择地区" filterable>
          <el-option v-for="item in regionList" :key="item.rid" :label="item.rname" :value="item.rid"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['cpi:cpi:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['cpi:cpi:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['cpi:cpi:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['cpi:cpi:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cpiList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="CPI编号" align="center" prop="cpiid" /> -->
      <el-table-column label="月份" align="center" prop="mname" />
      <el-table-column label="类别" align="center" prop="cname" />
      <el-table-column label="地区" align="center" prop="rname" />
      <el-table-column label="CPI" align="center" prop="cpi" sortable />
      <el-table-column label="创建时间" align="center" prop="createtime" />
      <el-table-column label="修改时间" align="center" prop="modifytime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['cpi:cpi:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['cpi:cpi:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改数据管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="月份" prop="mid">
          <!-- <el-input v-model="form.mid" placeholder="请输入月份" /> -->
          <el-select v-model="form.mid" placeholder="请选择月份" filterable>
            <el-option v-for="item in monthList" :key="item.mid" :label="item.mname" :value="item.mid"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类别" prop="cid">
          <!-- <el-input v-model="form.cid" placeholder="请输入类别" /> -->
          <el-select v-model="form.cid" placeholder="请选择类别">
            <el-option v-for="item in categoryList" :key="item.cid" :label="item.cname" :value="item.cid"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地区" prop="rid">
          <!-- <el-input v-model="form.rid" placeholder="请输入地区" /> -->
          <el-select v-model="form.rid" placeholder="请选择地区" filterable>
            <el-option v-for="item in regionList" :key="item.rid" :label="item.rname" :value="item.rid"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="CPI" prop="cpi">
          <el-input v-model="form.cpi" placeholder="请输入CPI" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCpi, getCpi, delCpi, addCpi, updateCpi, listMonth, listCategory, listRegion } from '@/api/cpi/cpi'

export default {
  name: 'Cpi',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据管理表格数据
      cpiList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        mid: null,
        cid: null,
        rid: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        mid: [{ required: true, message: '月份不能为空', trigger: 'blur' }],
        cid: [{ required: true, message: '类别不能为空', trigger: 'blur' }],
        rid: [{ required: true, message: '地区不能为空', trigger: 'blur' }],
        cpi: [{ required: true, message: 'CPI不能为空', trigger: 'blur' }]
      },
      // 月份表格数据
      monthList: [],
      // 分类表格数据
      categoryList: [],
      // 地区表格数据
      regionList: []
    }
  },
  created() {
    this.getList()
    this.getMonthList()
    this.getCategoryList()
    this.getRegionList()
  },
  methods: {
    /** 查询地区列表 */
    getRegionList() {
      // this.loading = true
      listRegion(this.queryParams)
        .then((response) => {
          this.regionList = response.rows
          // this.total = response.total;
          // this.loading = false;
        })
        .then(() => {
          this.regionList.pop()
        })
    },
    /** 查询分类列表 */
    getCategoryList() {
      // this.loading = true
      listCategory(this.queryParams).then((response) => {
        this.categoryList = response.rows
        // this.total = response.total;
        // this.loading = false;
      })
    },
    /** 查询月份列表 */
    getMonthList() {
      // this.loading = true
      listMonth(this.queryParams).then((response) => {
        this.monthList = response.rows
        // this.total = response.total;
        // this.loading = false;
      })
    },
    /** 查询数据管理列表 */
    getList() {
      this.loading = true
      listCpi(this.queryParams).then((response) => {
        this.cpiList = response.rows
        this.cpiList.forEach((item) => {
          // 字符串转为数字
          item.cpi = Number(item.cpi)
        })
        console.log(this.cpiList)
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        cpiid: null,
        mid: null,
        cid: null,
        rid: null,
        cpi: null,
        createtime: null,
        modifytime: null,
        deleted: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.cpiid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加数据管理'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const cpiid = row.cpiid || this.ids
      getCpi(cpiid).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改数据管理'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.cpiid != null) {
            updateCpi(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addCpi(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cpiids = row.cpiid || this.ids
      this.$modal
        .confirm('是否确认删除数据管理编号为"' + cpiids + '"的数据项？')
        .then(function () {
          return delCpi(cpiids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'cpi/cpi/export',
        {
          ...this.queryParams
        },
        `cpi_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
